namespace DynamicChart.Service.Interpreter;

public class ParamInterpreterEngine : IHostedService
{
    private static readonly Dictionary<string, IParamInterpreter> _map = new();
    private readonly DateTypeInterpreter _dateTypeInterpreter;

    public ParamInterpreterEngine(
        DateTypeInterpreter dateTypeInterpreter)
    {
        _dateTypeInterpreter = dateTypeInterpreter;
    }
    
    public void Register(IParamInterpreter interpreter) 
        => _map[interpreter.Name] = interpreter;

    public void Run(ref string value, IDictionary<string, object> param)
    {
        foreach (var interpreter in _map.Values)
        {
            interpreter.Interpret(ref value, param);
        }
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        Register(_dateTypeInterpreter);
        Register(new SetValueInterpreter());
        
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}